package br.ucb.sispro.control;

import java.util.List;

import javax.persistence.EntityManager;

import br.ucb.sispro.model.Entity;
import br.ucb.sispro.model.Estabelecimento;
import br.ucb.sispro.persistencia.GenericDAO;
import br.ucb.sispro.sets.SpringFactory;
import br.ucb.sispro.view.control.MessagesWeb;



public class Control<E extends Entity>  implements IControl<E>{

	private GenericDAO<E> persistence; 
	private ValidatorControl validator; 
	private MessagesControl messages;
	private Integer cont;

	@SuppressWarnings("unchecked") 
	public Control() { 
		persistence = (GenericDAO<E>) SpringFactory.getInstance().getBean("genericDAO", GenericDAO.class); 
		validator = new ValidatorControl(); 
		messages = new MessagesWeb();
	}

	@Override
	public List<E> getListAll(E entity) {
		// TODO Auto-generated method stub
		
		return persistence.getList(entity);
	}

	@Override
	public Long save(E entity) {
//		if (!validator.isEmpty(entity)) { 
//			if(!entity.getNameForMessages().equals("noMessage")){
//				messages.addMessage(entity.getNameForMessages());
//			}
			return persistence.save(entity);} 
//		else {
//			List<String> emptyFields = validator.getEmptyFields(); 
//			for(int i = 0; i < emptyFields.size(); i++){ 
//				messages.addMessage(emptyFields.get(i)); } 
//		} 
//		return (long) 0;
//	}

	@Override
	public void update(E entity) {
//		if (!validator.isEmpty(entity)) { 
			persistence.update(entity); 
			messages.addMessage("alteracao");
			//		}	 
//		else { 
//			List<String> emptyFields = validator.getEmptyFields(); 
//			for (int i = 0; i< emptyFields.size(); i++){ 
//				messages.addMessage(emptyFields.get(i)); 
//			} 
//		}

	}


	@Override
	public List<E> findByCriteria(E entity, String value) {
		if (!validator.isEmpty(value)) {
			return persistence.findByCriteria(entity, value); } 
		else { 
			messages.addMessage(validator.getEmptyFields().get(0)); 
		} return null;
	}

	@Override
	public List<E> findByHQL(E entity, String value) {
		if (!validator.isEmpty(value)) { 
			return persistence.findByHQL(entity, value); } 
		else { messages.addMessage(validator.getEmptyFields().get(0)); 
		} return null;
	}

	@Override
	public void delete(E entity) {
		persistence.delete(entity);
	}
	
	
	public GenericDAO<E> getPersistence() {
		return persistence;
	}

	public void setPersistence(GenericDAO<E> persistence) {
		this.persistence = persistence;
	}

	public ValidatorControl getValidator() {
		return validator;
	}

	public void setValidator(ValidatorControl validator) {
		this.validator = validator;
	}

	public MessagesControl getMessages() {
		return messages;
	}

	public void setMessages(MessagesControl messages) {
		this.messages = messages;
	}

	@Override
	public List<E> findByNamedQuery(String query,String valor) {
		return persistence.findByNamedQuery(query, valor);
	}

	@Override
	public E load(String entity, long id) {
		return persistence.load(entity, id);
	}

	@Override
	public List<E> findByHQLLivre(Entity entity, String campoPesquisa,
			String value) {
		return persistence.findByHQLLivre(entity, campoPesquisa, value);
	}
	
	
	public List<E> findByHQL(Entity entity,String campoPesquisa, String value){
		return persistence.findByHQL(entity, campoPesquisa, value);
	}

	
	public List<E> findSenhaByUser(Entity entity, String senha,String user) {
		return persistence.findSenhaByUser(entity, senha, user);
	}	
	
	
	public boolean verificaFuncao(String apelidoFuncao,long codUsuario){
		return persistence.verificaFuncao(apelidoFuncao, codUsuario);
	}
}
